# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=sfml
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.5.1
pkgrel=2
pkgdesc="A simple, fast, cross-platform, and object-oriented multimedia API (mingw-w64)"
arch=('any')
url="https://www.sfml-dev.org/"
license=("zlib")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
depends=("${MINGW_PACKAGE_PREFIX}-flac"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libvorbis"
         "${MINGW_PACKAGE_PREFIX}-openal")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "patch"
             "unzip")
source=("https://www.sfml-dev.org/files/SFML-${pkgver}-sources.zip"
        mingw-w64-msys2.patch)
sha256sums=('bf1e0643acb92369b24572b703473af60bac82caf5af61e77c063b779471bb7f'
            'd7f385419ca2451afc5b242382ed0e76d3ce1b9ee4cac47303bcb698c6801dfb')
noextract=(SFML-${pkgver}-sources.zip)

prepare() {
  rm -fr ${srcdir}/SFML-${pkgver}
  unzip SFML-${pkgver}-sources.zip
  cd "${srcdir}/SFML-${pkgver}"
  patch -p1 -i "${srcdir}/mingw-w64-msys2.patch"
}

build() {
  [[ -d "${srcdir}"/shared-${MINGW_CHOST} ]] && rm -rf ${srcdir}/shared-${MINGW_CHOST}
  mkdir -p "${srcdir}/shared-${MINGW_CHOST}" && cd "${srcdir}/shared-${MINGW_CHOST}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DSFML_BUILD_EXAMPLES=ON \
    -DSFML_BUILD_DOC=ON \
    -DSFML_INSTALL_PKGCONFIG_FILES=ON \
    -DBUILD_SHARED_LIBS=ON \
    ../SFML-${pkgver}

  make
  make doc

  [[ -d "${srcdir}"/static-${MINGW_CHOST} ]] && rm -rf ${srcdir}/static-${MINGW_CHOST}
  mkdir -p "${srcdir}/static-${MINGW_CHOST}" && cd "${srcdir}/static-${MINGW_CHOST}"
  
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DSFML_BUILD_EXAMPLES=OFF \
    -DSFML_BUILD_DOC=OFF \
    -DSFML_INSTALL_PKGCONFIG_FILES=ON \
    -DBUILD_SHARED_LIBS=OFF \
    ../SFML-${pkgver}

  make
}

package() {
  cd "${srcdir}/static-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install

  cd "${srcdir}/shared-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install

  install -Dm644 "${srcdir}/SFML-${pkgver}/license.md" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
